<?
/**
 * 新闻
 */
class News extends MyBase{
	
	function __construct() {
		parent::__construct('news.id', array('keyword' => 'title'));
	}
	
	/**
	 * 修改新闻
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('title' => $post['title']);
		if($id > 0){
			$select['kept'] = " AND `id`!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){
			return $this->rwarn(lang('already') . lang('exist'));
		}
		if($id > 0){
			$news = $this->get_row($id, 'id,filename,addtime');
		}
		if($post['rank'] > 1000000000)$post['rank'] = 1000000000;
		if(isset($post['ontop']))$post['rank'] = 1000000000;
		if(check($post['cut_from_content'], 1) || empty($post['summary']))$post['summary'] = String::summary($post['content'], SUMMARY_LEN);
		$post['bold'] = empty($post['bold']) ? '' : $post['bold'];
		$data = array(
			'catid'		=> $post['catid'],
			'title'		=> $post['title'],
			'style'		=> $post['color'].','.$post['bold'],
			'publish'	=> $post['publish'] == 1 ? 1 : 0,
			'rank'		=> $post['rank'],
			'filename'	=> $post['filename'],
			'url'		=> $post['url'],
			'summary'	=> $post['summary'],
			'content'	=> $post['content'],
		);
		$data['addtime'] = empty($post['addtime']) ? TIME : strtotime($post['addtime']);
		if($id > 0){
			$data['updatetime'] = empty($post['updatetime']) ? TIME : strtotime($post['updatetime']); 
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			if(isset($news) && !empty($news)){
				$this->delete_html_file($id, $news['addtime'], $news['filename']);
			}
		}
		return $r;
	}
		
	/**
	 * 更新排序
	 */
	/*public function rank($post){
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			$data = array(
				'rank'		 => $rank, 
				'updatetime' => TIME 
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $iRtn;
	}*/
	
	/**
	 * 删除新闻
	 */
	public function del($post){
		$iRtn = 0;
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		foreach($post['list'] as $id){
			$news = $this->get_row($id, 'id,filename,addtime');
			if(empty($news) && empty($news['id']))continue;
			if(!$this->db->update($this->tbname, array('id' => $id), array('isdel' => 1))){
				return -1;
			}
			$iRtn ++;
			$this->delete_html_file($id, $news['addtime'], $news['filename']);
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*') {
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	public function get_by_filename($filename, $fields = '*'){
		return $this->db->get_row($this->tbname, array('filename' => $filename), $fields);
	}
	
	public function get_prev($rank, $id, $fields = '*'){
		$row = $this->db->sql_row("SELECT {$fields} FROM `tb_news` WHERE `rank`>={$rank} AND `id`>{$id} ORDER BY `rank` ASC,id ASC");
		if(empty($row)){
			$row = $this->db->sql_row("SELECT {$fields} FROM `tb_news` WHERE `rank`>{$rank} AND `id`<{$id} ORDER BY `rank` ASC,id ASC");
		}
		return $row;
	}
	
	public function get_next($rank, $id, $fields = '*'){
		$row = $this->db->sql_row("SELECT {$fields} FROM `tb_news` WHERE `rank`<={$rank} AND `id`<{$id} ORDER BY `rank` DESC,id DESC");
		if(empty($row)){
			$row = $this->db->sql_row("SELECT {$fields} FROM `tb_news` WHERE `rank`<{$rank} AND `id`>{$id} ORDER BY `rank` DESC,id DESC");
		}
		return $row;
	}
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET) {
		if(empty($select['catid'])){
			unset($select['catid']);
		}
		if(!empty($select['keyword'])){
			$select['kept'] = " AND `title` LIKE '%{$select['keyword']}%'";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
		
	/********************************************************************************
	 * 更新静态文件
	 ********************************************************************************/
	
	public function mkhtml($post){
		if(empty($post['list'])){
			return true;
		}
		return $this->update_html_files($post['list']);
	}

	public function update_html_files($list){
		if(in_array('news_content', $list)){
			$news_content = true;
		}else{
			$news_content = false;
		}
		foreach ($list as $key){
			if($key == 'index'){
				File::delFile(WEB_PTH.'index.html');
			}elseif($key == 'news_index'){
				$this->delete_html_list(0, $news_content);
			}else{
				$this->delete_html_list($key, $news_content);
			}
		}
		return true;
	}
	
	private function delete_html_file($id, $addtime, $filename = ''){
		if(!empty($filename)){
			File::delFile(PUB_PTH.'news/'.$filename.'.html');
		}
		File::delFile(PUB_PTH.'news/'.time_to_path($addtime).'/'.$id.'.html');
	}
	
	private function delete_html_list($key, $news_content = false){
		$catid = intval(str_replace('news_category_', '', $key));
		if($catid > 0){
			if($news_content == true){
				$list = $this->get_list('id,filename,addtime', array('catid' => $catid), array(), 1, 2000);
				if(isset($list['list']) && is_array($list['list'])){
					foreach($list['list'] as $row){
						$this->delete_html_file($row['id'], $row['addtime'], $row['filename']);
					}
				}
			}
			
			$news_categorys = cache_read('news_categorys.php');
			$mark = $news_categorys['category_list'][$catid]['mark'].'/';
		}else{
			$mark = '';
		}
		$files = File::getFiles(PUB_PTH.'news/'.$mark, false);
		foreach ($files as $file){
			File::delFile(PUB_PTH.'news/'.$mark.$file);
		}
	}
}
?>